/*
 * Copyright 2011 Jin Kwon <jinahya at gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.googlecode.flac4j.format.metadata;


import com.googlecode.flac4j.format.FormatHelper;
import com.googlecode.jinahya.io.BitInput;
import com.googlecode.jinahya.io.BitOutput;

import java.io.IOException;

import java.nio.charset.Charset;
import java.util.Arrays;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;


/**
 *
 * @author Jin Kwon <jinahya at gmail.com>
 */
@XmlType(propOrder = {"id", "data"})
public class Application extends AbstractData {


    private static final int ID_LENGTH_IN_BYTES = 4;


    private static final Charset US_ASCII = Charset.forName("US-ASCII");


    public static Data newInstance(final int id, final byte[] data) {

        final Application instance = new Application();
        instance.setId(id);
        instance.setData(data);

        return instance;
    }


    /**
     * Creates a new instance.
     */
    public Application() {
        super(BlockType.APPLICATION);
    }


    @Override
    public final int getLengthInBytes() {
        return ID_LENGTH_IN_BYTES + data.length;
    }


    @Override
    public void read(final BitInput input) throws IOException {

        setId(input.readInt(32));

        data = new byte[getHeader().getDataLength() - ID_LENGTH_IN_BYTES];
        FormatHelper.readBytes(input, data);
    }


    @Override
    public void write(final BitOutput output) throws IOException {

        output.writeInt(32, getId());

        FormatHelper.writeBytes(output, data);
    }


    // ---------------------------------------------------------------------- id
    /**
     * Returns id.
     *
     * @return id
     */
    public int getId() {

        return id;
    }


    /**
     * Sets id.
     *
     * @param id id.
     */
    public void setId(final int id) {

        this.id = id;
    }


    /**
     * Returns id as a String.
     *
     * @return id as a String
     */
    @XmlAttribute
    public String getIdAsString() {

        return new String(FormatHelper.intToBytes(getId()), US_ASCII);
    }


    /**
     * Sets id as an integer.
     *
     * @param id id as an integer.
     */
    public void setIdAsString(final String id) {

        if (id == null) {
            throw new NullPointerException("null id");
        }

        final byte[] bytes = id.getBytes(US_ASCII);
        if (bytes.length != 4) {
            throw new IllegalArgumentException(
                "id.ascii.length(" + bytes.length + ") != 4");
        }

        setId(FormatHelper.bytesToInt(bytes));
    }


    // -------------------------------------------------------------------- data
    /**
     * Returns data.
     *
     * @return data.
     */
    public byte[] getData() {
        return Arrays.copyOf(data, data.length);
    }


    /**
     * Sets data.
     *
     * @param data data.
     */
    public void setData(final byte[] data) {

        if (data == null) {
            throw new NullPointerException("null data");
        }

        this.data = Arrays.copyOf(data, data.length);
    }


    /** id. */
    @XmlElement(required = true)
    private int id;


    /**
     * n '0' bits (n must be a multiple of 8).
     */
    @XmlElement(required = true)
    @XmlSchemaType(name = "base64Binary")
    private byte[] data;


}

